﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace OnlineGame
{
   public static class Server
    {
       public static int userID;
       public static string currentUser;
       private static string sConnection = "Server=kimkazserver.no-ip.info;Database=onlinegame;Uid=kaz;Pwd=123456;";
       private static MySqlConnection oConn;

        public static MySqlConnection Connect()
        {
            try
            {
                oConn = new MySqlConnection(sConnection);
                oConn.Open();
            }
            catch (MySqlException e)
            {
                throw e;
            }
            return oConn;
        }// Connect()
       
       public static void createAccount(String users,String password)
       {
            MySqlCommand connection;
            try
            {
                if (!checkUserExistence(users))
                {
                    connection = new MySqlCommand("INSERT INTO users(Login,Password)" + 
                        " VALUES("
                            + null            +
                        "'" + users     + "',"     +
                        "'" + password  + "')",
                        oConn);

                    connection.ExecuteScalar();        
                    System.Windows.Forms.MessageBox.Show("Conta criada com Sucesso");
                }
            }
            catch (MySqlException e)
            {
                throw e;
            }
        }//Create Account()

       private static bool checkUserExistence(string Login)
       {
           try
           {
               MySqlCommand checkUser
                   = new MySqlCommand("SELECT ID FROM users WHERE login='" + Login + "'", oConn);
               if (checkUser.ExecuteScalar() != null)
               {
                   System.Windows.Forms.MessageBox.Show("Este login já está sendo usado");
                   return true;
               }
               else
               {
                   return false;
               }
           }
           catch (MySqlException e)
           {
               throw e;
           }
       }

       private static bool checkPlayersOnline()
       {
           return false;
       }
       public static void CreateChar(string charName, string bodyColor)
       {
           try
           {

               MySqlCommand getUser = new MySqlCommand("Select ID FROM Users WHERE Login='" + currentUser + "'",oConn);
               string curUser = getUser.ExecuteScalar().ToString();
               MySqlCommand insertChar = new MySqlCommand("INSERT INTO characters(UserID,BodyColor) VALUES("
                   + "'" + curUser + "',"
                   + "'" + bodyColor + "')", oConn);
               insertChar.ExecuteNonQuery();
               System.Windows.Forms.MessageBox.Show("Char criado com sucesso");
               Game1.currentState = Game1.gameStates.CHARACTERS;
           }
           catch (MySqlException e)
           {
               throw e;
           }
       }

        public static bool checkLogin(String user, String password)
        {
            try
            {
                MySqlCommand connection2;
                connection2 = new MySqlCommand("SELECT password FROM users where login = '" + user + "'", oConn);
                connection2.ExecuteScalar();
                if (connection2.ExecuteScalar() != null)
                {
                    string pass = connection2.ExecuteScalar().ToString();
                    if (password == pass)
                    {
                        currentUser = user;
                        return true;                        
                    }
                    else
                    {
                        return false;
                    }
                }
                return false;
            }
            catch (MySqlException e)
            {
                return false;
                throw e;
            }
        }

    }
}
